/****************************************************************************
**
** Copyright (C) 2013 Aaron McCarthy <mccarthy.aaron@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
** 2015.4.4
** Adapted for use with QGroundControl
**
** Gus Grubba <gus@auterion.com>
**
****************************************************************************/

#ifndef QGEOMAPREPLYQGC_H
#define QGEOMAPREPLYQGC_H

#include <QtNetwork/QNetworkReply>
#include <QtLocation/private/qgeotiledmapreply_p.h>
#include <QTimer>

#include "QGCMapEngineData.h"

/**
 * @brief QGC地图瓦片回复类
 * 
 * QGeoTiledMapReplyQGC是QtLocation模块中用于处理地图瓦片请求回复的类，
 * 它继承自QGeoTiledMapReply，专门用于QGroundControl项目。
 * 
 * 该类主要负责：
 * 1. 处理地图瓦片的网络请求和回复
 * 2. 管理地图瓦片的缓存读取和存储
 * 3. 处理不同类型的地图瓦片（普通地图、高程图等）
 * 4. 处理特殊地图提供商（如Bing地图）的特殊情况
 * 5. 管理请求超时和错误处理
 * 
 * 工作流程：
 * 1. 首先尝试从本地缓存获取瓦片
 * 2. 如果缓存中没有，则发起网络请求获取瓦片
 * 3. 获取到瓦片后进行处理并缓存
 * 4. 将结果返回给地图显示系统
 */
class QGeoTiledMapReplyQGC : public QGeoTiledMapReply
{
    Q_OBJECT
public:
    /**
     * @brief 构造函数
     * @param networkManager 网络管理器
     * @param request 网络请求
     * @param spec 瓦片规格信息
     * @param parent 父对象
     */
    QGeoTiledMapReplyQGC(QNetworkAccessManager*  networkManager, const QNetworkRequest& request, const QGeoTileSpec &spec, QObject *parent = 0);
    
    /**
     * @brief 析构函数
     */
    ~QGeoTiledMapReplyQGC();
    
    /**
     * @brief 中止请求
     */
    void abort();

signals:
    /**
     * @brief 高程数据获取完成信号
     * @param responseBytes 响应数据
     * @param error 网络错误代码
     */
    void terrainDone            (QByteArray responseBytes, QNetworkReply::NetworkError error);

private slots:
    /**
     * @brief 网络请求完成处理槽函数
     *        cacheError触发后会建立网络请求，请求完成后会进入这个函数
     */
    void networkReplyFinished   ();
    
    /**
     * @brief 网络请求错误处理槽函数
     *        cacheError触发后会建立网络请求，请求错误后会进入这个函数
     * @param error 网络错误代码
     */
    void networkReplyError      (QNetworkReply::NetworkError error);
    
    /**
     * @brief 缓存回复处理槽函数
     * @param tile 缓存的瓦片数据
     */
    void cacheReply             (QGCCacheTile* tile);
    
    /**
     * @brief 缓存错误处理槽函数, 查找缓存错误后，建立网络请求去获取图片
     * @param type 任务类型
     * @param errorString 错误信息
     */
    void cacheError             (QGCMapTask::TaskType type, QString errorString);
    
    /**
     * @brief 请求超时处理槽函数
     */
    void timeout                ();

private:
    /**
     * @brief 清理网络回复资源
     */
    void _clearReply            ();

private:
    QNetworkReply*          _reply;              ///< 网络回复对象
    QNetworkRequest         _request;            ///< 网络请求对象
    QNetworkAccessManager*  _networkManager;     ///< 网络管理器
    QByteArray              _badMapbox;          ///< 错误的Mapbox瓦片数据
    QByteArray              _badTile;            ///< 错误的瓦片数据
    QTimer                  _timer;              ///< 超时定时器
    static QByteArray       _bingNoTileImage;    ///< Bing地图空瓦片图像数据
    static int              _requestCount;       ///< 当前请求数量计数器
};

#endif // QGEOMAPREPLYQGC_H